# import cv2
# import time

# cap = cv2.VideoCapture(0)
# if not cap.isOpened():
#     print("The camera is not opened")
#     exit(1)

# img_counter = 0
# while True:
#     ret, frame = cap.read()
#     if not ret:
#         print("Failed to grab frame")
#         break
#     img_name = f"{img_counter % 7}.jpg"  # 使用模运算保证图片名称在0-6之间
#     cv2.imwrite(img_name, frame)
#     print(f"{img_name} written!")

#     img_counter += 1

#     # 等待 1 秒
#     time.sleep(1)

# cap.release()
import cv2
import time
from datetime import datetime

def add_watermark(image, text, position, font=cv2.FONT_HERSHEY_SIMPLEX, 
                  font_scale=1, font_color=(255, 255, 255), line_type=2):
    cv2.putText(image, text, position, font, font_scale, font_color, line_type)

cap = cv2.VideoCapture(0)
if not cap.isOpened():
    print("The camera is not opened")
    exit(1)

img_counter = 0
while True:
    ret, frame = cap.read()
    if not ret:
        print("Failed to grab frame")
        break

    # 获取当前时间并格式化
    timestamp = datetime.now().strftime("%Y:%m:%d-%H:%M:%S")
    # 设置水印位置在右下角
    height, width = frame.shape[:2]
    position = (width - 400, height - 20)

    # 添加水印
    add_watermark(frame, timestamp, position)

    img_name = f"{img_counter % 7}.jpg"  # 使用模运算保证图片名称在0-6之间
    cv2.imwrite(img_name, frame)
    print(f"{img_name} written!")

    img_counter += 1

    # 等待 1 秒
    time.sleep(1)

cap.release()
